import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * 注意：子菜单仅在路由子项时显示。长度>=1
 * 详情见: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * 数据字段解释
 *
 * hidden: true                   如果设置为true，项目将不会显示在侧栏中（默认值为false）
 * alwaysShow: true               如果设置为true，将始终显示根菜单
 *                                如果未设置alwaysShow，则当项目有多个子路由时，
 *                                它将成为嵌套模式，否则不显示根菜单
 * redirect: noRedirect           如果设置为noRedirect，则不会在面包屑中重定向
 * name:'router-name'             名称由<keep alive>使用（必须设置！！！）
 * meta : {
    roles: ['admin','editor']    控制页面角色（可以设置多个角色）
    title: 'title'               名称显示在侧栏和面包屑中（推荐集）
    icon: 'svg-name'/'el-icon-x' 侧栏中显示的图标
    breadcrumb: false            如果设置为false，该项将隐藏在breadcrumb中（默认值为true）
    activeMenu: '/example/list'  如果设置路径，侧栏将突出显示您设置的路径
  }
 */

/**
 * 没有做CMS权限
 * 没有权限要求的基本页
 * 可以访问所有角色
 */

export const constantRoutes = [
  // 登陆
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 404页面
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  // 表格模版
  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: '表格模版', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: '数据列表', icon: 'table' }
      }
    ]
  },

  // 表单
  {
    path: '/form',
    component: Layout,
    redirect: '/form/index1',
    name: 'Form',
    meta: { title: '表单/嵌套', icon: 'form' },
    children: [
      {
        path: 'index1',
        name: 'Form1',
        component: () => import('@/views/form/index'),
        meta: { title: '表单1' }
      },
      {
        path: 'index2',
        name: 'Form2',
        component: () => import('@/views/form/index'),
        meta: { title: '表单2' },
        children: [
          {
            path: 'index2-1',
            name: 'Form2-1',
            component: () => import('@/views/form/index'),
            meta: { title: '表单2-1' }
          },
          {
            path: 'index2-2',
            name: 'Form2-2',
            component: () => import('@/views/form/index'),
            meta: { title: '表单2-2' }
          }
        ]
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '跳转链接', icon: 'link' }
      }
    ]
  },

  // 404页必须放在末尾 !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
